package tt.dz.appserver.service.business.imp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.avro.AvroRemoteException;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.dao.ext.StationCommentDao;
import tt.dz.appserver.entity.vo.OngoingOperation;
import tt.dz.appserver.entity.vo.ReservationDetailVo;
import tt.dz.appserver.entity.vo.charge.ChargeDetailVo;
import tt.dz.appserver.entity.vo.charger.ChargerDetailVo;
import tt.dz.appserver.entity.vo.charger.DeviceTypeInfoVo;
import tt.dz.appserver.entity.vo.station.StationDetailVo;
import tt.dz.appserver.service.avro.client.oms.dzttOmsServiceProtocol;
import tt.dz.appserver.service.business.ChargeService;
import tt.dz.appserver.util.CodeUtils;
import tt.dz.appserver.util.Constants;
import tt.dz.appserver.util.dzcode.OmsCode;
import tt.dz.appserver.util.enums.Response;
import tt.dz.appserver.util.json.JsonUtils;
import tt.dz.base.common.utils.DateUtil;

/**
 * 充电业务实现类
 * 
 * @author Administrator
 *
 */
public class ChargeServiceImpl implements ChargeService {

	/** 日志 */
	private static final Logger LOG = Logger.getLogger("[充电接口]:");

	/** OMS rpc接口 */
	@Resource(name = "omsMessageProtocolRequestor")
	private dzttOmsServiceProtocol omsService;
	
	@Resource
	private StationCommentDao stationCommentDao;

	@Override
	public JSONObject startCharge(String userCard, String chargerId, String chargePlugNo) throws AvroRemoteException {

		JSONObject resultJsonObject = new JSONObject();

		// 调用开启充电
		String resultJsonString = omsService.startCharge(userCard, chargerId, chargePlugNo).toString();
			
		LOG.info("调用充电startCharge"+DateUtil.getDateTimeLong()+"【" + userCard + "】卡 用户开始 " + chargerId + "桩 " + chargePlugNo + "插头 【预约】充电,返回结果:" + resultJsonString);

		// json转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);

		// 获取操作结果代码
		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);
		LOG.info("调用充电startCharge"+DateUtil.getDateTimeLong()+"返回结果【" + resultJsonString + "】");

		if (OmsCode.SUCCESS.equals(code)) {
			resultJsonObject=JsonUtils.getSuccessJson();
		} else {
			Response response = CodeUtils.getCodeByOMSServerCode(code);
			resultJsonObject=JsonUtils.getFailJson(response);
		}
		return resultJsonObject;
	}

	/**
	 * 扫码充电实现
	 */
	@Override
	public JSONObject scanCharge(String userCard, String qrCode, String chargePlugNo) throws AvroRemoteException {

		JSONObject resultJsonObject = new JSONObject();
		LOG.info(" 扫码充电实现服务Serverdate="+DateUtil.getDateTimeLong()+"userCard="+userCard+"qrCode="+qrCode+"chargePlugNo:"+chargePlugNo);
		// 调用开启充电
		String resultJsonString = omsService.startChargeByQrCode(qrCode, userCard, chargePlugNo).toString();

		LOG.info(DateUtil.getDateTimeLong()+"开始二维码为【 " + qrCode + "】桩 " + chargePlugNo + "插头 【扫码】充电,OMS返回结果:" + resultJsonString);

		// json转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);

		// 获取操作结果代码
		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);
		LOG.info("startCharge"+DateUtil.getDateTimeLong()+"返回结果【" + resultJsonString + "】");
		if (OmsCode.SUCCESS.equals(code)) {
			resultJsonObject=JsonUtils.getSuccessJson();
		} else {
			Response response = CodeUtils.getCodeByOMSServerCode(code);
			resultJsonObject=JsonUtils.getFailJson(response);
		}
		return resultJsonObject;
	}

	@Override
	public JSONObject closeCharge(String chargeSn) throws AvroRemoteException {

		JSONObject resultJsonObject = null;

		// 调用关闭充电
		String resultJsonString = omsService.closeCharge(chargeSn).toString();

		LOG.info("关闭 【" + chargeSn + "】充电流水号,OMS返回结果:" + resultJsonString);

		// json转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);

		// 获取操作结果代码
		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);

		if (OmsCode.SUCCESS.equals(code)) {
			resultJsonObject=JsonUtils.getSuccessJson();
		} else {
			Response response = CodeUtils.getCodeByOMSServerCode(code);
			resultJsonObject=JsonUtils.getFailJson(response);
		}
		return resultJsonObject;
	}

	@Override
	public JSONObject chargeList(String userCard, Integer pageNo) throws AvroRemoteException {

		JSONObject resultJsonObject = null;

		// 获取充电列表
		String resultJsonString = omsService.getChargeListByPage(userCard, pageNo).toString();

		LOG.info("查询【" + userCard + "】卡号充电列表,OMS返回结果:" + resultJsonString);

		// json转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);

		// 获取操作结果代码
		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);

		if (OmsCode.SUCCESS.equals(code)) {

			JSONArray chargeList = omsResultObject.getJSONArray("list");
			int chargersCount = chargeList.size();

			List<ChargeDetailVo> chargeDetailVos = new ArrayList<>();
			for (int i = 0; i < chargersCount; i++) {
				JSONObject charge = chargeList.getJSONObject(i);
				//封装字段
				ChargeDetailVo chargeDetailVo = JsonUtils.JsonToObjectOfClass(charge, ChargeDetailVo.class);
				chargeDetailVos.add(chargeDetailVo);
			}
			resultJsonObject=JsonUtils.getSuccessJson();
			resultJsonObject.put("chargeDetailVo", chargeDetailVos);
		} else {
			Response response = CodeUtils.getCodeByOMSServerCode(code);
			resultJsonObject=JsonUtils.getFailJson(response);
		}
		return resultJsonObject;
	}

	@Override
	public JSONObject chargeDetail(String chargeSn) throws AvroRemoteException {

		JSONObject resultJsonObject = null;

		// 获取充电详细
		String resultJsonString = omsService.getChargeDetailById(chargeSn).toString();

		LOG.info("查询【" + chargeSn + "】流水号充电详细,OMS返回结果:" + resultJsonString);

		// json转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);

		// 获取操作结果代码
		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);

		if (OmsCode.SUCCESS.equals(code)) {

			JSONObject charge = omsResultObject.getJSONObject("pojo");

			//封装充电详细字段
			ChargeDetailVo chargeDetailVo = JsonUtils.JsonToObjectOfClass(charge, ChargeDetailVo.class);
			
			
			/**=========================================获取电站详细 begin====================================================*/
			String stationId = chargeDetailVo.getStationId();
			if(stationId==null){
				LOG.error("获取到的站点id为空");
				Response response = CodeUtils.getCodeByOMSServerCode(code);
				resultJsonObject=JsonUtils.getFailJson(response);
				return resultJsonObject;
			}
			String stationJsonString = omsService.getStationDetailByStationId(stationId).toString();
			// json转换
			JSONObject stationResultObject = JSONObject.parseObject(stationJsonString);

			String stationCode = stationResultObject.getString(Constants.OmsServerResultField.CODE);

			LOG.info("获取 【" + stationId + "】 站点详细,OMS返回:" + stationJsonString);
			if (OmsCode.SUCCESS.equals(stationCode)) {
				// 获取站点详细
				JSONObject stationObject = stationResultObject.getJSONObject("pojo");
				StationDetailVo stationDetailVo = JsonUtils.JsonToObjectOfClass(stationObject, StationDetailVo.class);
				String grade = stationCommentDao.findStationCommentGrade(stationId);
				stationDetailVo.setGrade(String.valueOf(grade));
				
				//封装站点详细
				chargeDetailVo.setStationDetail(stationDetailVo);
			}
			/**=========================================获取电站详细 End====================================================*/
			

			/**=========================================获取桩详细 begin====================================================*/
			String chargerId = chargeDetailVo.getChargerId();
			
			String chargerJsonString = omsService.getChargerDetailByChargerId(chargerId).toString();
			// json转换
			JSONObject chargerResultObject = JSONObject.parseObject(chargerJsonString);
			// 获取操作结果
			String chargerCode = chargerResultObject.getString(Constants.OmsServerResultField.CODE);
			LOG.info("获取电桩ID为【" + chargerId + "】 电桩详细接口,OMS返回:" + chargerJsonString);

			// 操作成功
			if (OmsCode.SUCCESS.equals(chargerCode)) {
				JSONObject charger = chargerResultObject.getJSONObject("pojo");
				// 封装字段
				ChargerDetailVo chargerDetailVo = JsonUtils.JsonToObjectOfClass(charger, ChargerDetailVo.class);
				//设备类型
				JSONObject deviceTypeJson = charger.getJSONObject("deviceType");
				if(deviceTypeJson!=null){
					DeviceTypeInfoVo deviceTypeVo = JsonUtils.JsonToObjectOfClass(deviceTypeJson, DeviceTypeInfoVo.class);
					chargerDetailVo.setDeviceType(deviceTypeVo);
				}
				//封装电桩详细
				chargeDetailVo.setChargerDetail(chargerDetailVo);
			}else{
				ChargerDetailVo chargerDetailVo = new ChargerDetailVo();
				chargeDetailVo.setChargerDetail(chargerDetailVo);
			}
			/**=========================================获取桩详细 End====================================================*/

			resultJsonObject=JsonUtils.getSuccessJson();
			resultJsonObject.put("chargeDetail", chargeDetailVo);
		} else {

			Response response = CodeUtils.getCodeByOMSServerCode(code);
			resultJsonObject=JsonUtils.getFailJson(response);
		}
		return resultJsonObject;
	}

	@Override
	public JSONObject getOngoingOperation(String userPayCard) throws AvroRemoteException {
		
		JSONObject resultJsonObject = null;

		// 调用开启充电
		String resultJsonString = omsService.getChargeInfoAndBespeakInfoByUserPayCard(userPayCard).toString();
		
		LOG.info("查询【" + userPayCard + "】卡号正在进行的业务:" + resultJsonString);

		// json转换
		JSONObject omsResultObject = JSONObject.parseObject(resultJsonString);

		// 获取操作结果代码
		String code = omsResultObject.getString(Constants.OmsServerResultField.CODE);

		if (OmsCode.SUCCESS.equals(code)) {
			
			resultJsonObject = JsonUtils.getSuccessJson();
			
			//获取数据
			JSONObject operations = omsResultObject.getJSONObject("pojo");
			
			OngoingOperation operation = null;
			
			//如果有正在进行的业务
			if(operations!=null){
				
				operation = new OngoingOperation();
				//充电
				JSONArray chargeArray = operations.getJSONArray("charge");
				//预约
				JSONArray reservationArray = operations.getJSONArray("bespeak");
				
				//充电
				List<ChargeDetailVo> charges = null;
				if(chargeArray!=null){
					charges = new ArrayList<ChargeDetailVo>();
					for (int i = 0; i < chargeArray.size(); i++) {
						JSONObject charge = chargeArray.getJSONObject(i);
						ChargeDetailVo chargeDetailVo = JsonUtils.JsonToObjectOfClass(charge, ChargeDetailVo.class);
						ChargerDetailVo chargerDetail = getChargerById(chargeDetailVo.getChargerId());
						chargeDetailVo.setChargerDetail(chargerDetail);
						charges.add(chargeDetailVo);
						
					}
				}

				//预约
				List<ReservationDetailVo> reservations = null;
				if(reservationArray!=null){
					
					reservations = new ArrayList<ReservationDetailVo>();
					for (int i = 0; i < reservationArray.size(); i++) {
						JSONObject reservation = reservationArray.getJSONObject(i);
						ReservationDetailVo reservationDetailVo = JsonUtils.JsonToObjectOfClass(reservation, ReservationDetailVo.class);
						ChargerDetailVo chargerDetail = getChargerById(reservationDetailVo.getChargerId());
						reservationDetailVo.setChargerDetail(chargerDetail);
						reservations.add(reservationDetailVo);
					}
				}
				
				//设置结果
				operation.setChargeList(charges);
				operation.setReservationList(reservations);
			}
			resultJsonObject.put("operation", operation);
		}
		else{
			Response response = CodeUtils.getCodeByOMSServerCode(code);
			resultJsonObject = JsonUtils.getFailJson(response);
		}
		return resultJsonObject;
	}
	
	private ChargerDetailVo  getChargerById(String chargerId) throws AvroRemoteException{
		
		ChargerDetailVo chargerDetailVo=null;
		String chargerJsonString = omsService.getChargerDetailByChargerId(chargerId).toString();
		// json转换
		JSONObject chargerResultObject = JSONObject.parseObject(chargerJsonString);
		// 获取操作结果
		String chargerCode = chargerResultObject.getString(Constants.OmsServerResultField.CODE);
		LOG.info("获取电桩ID为【" + chargerId + "】 电桩详细接口,OMS返回:" + chargerJsonString);

		// 操作成功
		if (OmsCode.SUCCESS.equals(chargerCode)) {
			JSONObject charger = chargerResultObject.getJSONObject("pojo");
			// 封装字段
			 chargerDetailVo = JsonUtils.JsonToObjectOfClass(charger, ChargerDetailVo.class);
			//设备类型
			JSONObject deviceTypeJson = charger.getJSONObject("deviceType");
			if(deviceTypeJson!=null){
				DeviceTypeInfoVo deviceTypeVo = JsonUtils.JsonToObjectOfClass(deviceTypeJson, DeviceTypeInfoVo.class);
				chargerDetailVo.setDeviceType(deviceTypeVo);
			}
			//封装电桩详细
		}else{
			 chargerDetailVo = new ChargerDetailVo();
		}
		return  chargerDetailVo;
		
	}
	
	
}
